//
//  ZZStatusOriginalView.swift
//  sina
//
//  Created by 张鹏 on 16/5/26.
//  Copyright © 2016年 ZP. All rights reserved.
//

import UIKit

let StatusCellMargin: CGFloat = 8

class ZZStatusOriginalView: UIView {

    var status : ZZStatus? {
        
        didSet{
            //数据绑定
             //TODO: 未完待续..........
            let url = NSURL(string: status?.user?.profile_image_url ?? "")
            headIcon.sd_setImageWithURL(url)
           
            //认证图片
            if status?.user?.verified == 0 {
                verifiedImageView.hidden = true
            }
            else {
                verifiedImageView.hidden = false
            
            }
            //姓名
            nameLable.text = status?.user?.name

            //用户等级
            if status?.user?.mbrank > 0 && status?.user?.mbrank < 7 {
        //TODO:  status?.user?.mbrank ?? 0  : 后面?? 0 在转译的时候会吧可选项中的Optional(5)转化为5
                let image = UIImage(named: "common_icon_membership_level\(status?.user?.mbrank ?? 0)")
 
                mbrankImageView.image = image
            }
            
            //时间
            createdImage.text = NSDate.sinaDate((status?.created_at)!)?.fullDescription()
              
            //来自
//            sourceLable.text = status?.source
            
            sourceLable.text = viewModel(status: status!).sourceText
            
            //内容
            textLable.text = status?.text
            
            //配图视图的字典转模型赋值
            pictureView.pictures = status?.pic_urls
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.yellowColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI界面
    private func setupUI() {
        
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(nameLable)
        addSubview(mbrankImageView)
        addSubview(createdImage)
        addSubview(sourceLable)
        addSubview(textLable)
        addSubview(pictureView)
        
        headIcon.snp_makeConstraints { (make) in
            
            make.top.left.equalTo(self).offset(StatusCellMargin)
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(headIcon.snp_bottom)
            make.right.equalTo(headIcon.snp_right)
        }
        
        nameLable.snp_makeConstraints { (make) in
        
            make.top.equalTo(self).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            
            make.top.equalTo(self).offset(StatusCellMargin)
            make.left.equalTo(nameLable.snp_right).offset(StatusCellMargin)
        }
        
        createdImage.snp_makeConstraints { (make) in
            
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_left)
            
        }
        
        sourceLable.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(createdImage.snp_right).offset(StatusCellMargin)
        }
        
        textLable.snp_makeConstraints { (make) in
            
            make.top.equalTo(createdImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(textLable.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLable.snp_left)
            //设置测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        //给原创微博底部添加约束
            self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
        }
  
    }
    
    //懒加载
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var verifiedImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var nameLable : UILabel = UILabel(title: "小宝", font: 14, textColor: UIColor.darkGrayColor())

    private lazy var mbrankImageView : UIImageView  = UIImageView(image: UIImage(named: "common_icon_membership"))

    private lazy var createdImage :UILabel = UILabel(title: "时间", font: 10, textColor: UIColor.grayColor(), alignment: .Center)


    private lazy var sourceLable : UILabel = UILabel(title: "来自", font: 10, textColor: UIColor.grayColor())
    
    
    private lazy var textLable : UILabel = UILabel(title: "haha", font: 10, textColor: UIColor.grayColor(), alignment: .Left)

    //配图视图
    private lazy var pictureView : ZZStatusPictureView = ZZStatusPictureView()
    
    
    //转换日期格式的方法,用字符串接收返回值
    var time :String? {
        
        return NSDate.sinaDate((status?.created_at)!)?.fullDescription()
    }
}





